const router = require("express-promise-router")();
const express = require('express');
const UserService = require("../services/user");

/**
 * @openapi
 * /api/v1/login:
 *   post:
 *     tags:
 *     - emooc
 *     summary: 第三方程序,如硬木盒子登录.使用密码明文登录
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *               password:
 *                 type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 成功，返回user
 *       400:
 *         description: 失败 {error:"账号密码无效"}
 */
//第三方,如硬木盒子登录.使用密码明文登录
router.post('/login', async function (req, res) {
    const {username, password} = req.body;
    try {
        const user = await UserService.verifyByPassword(username, password);
        delete user.password;//返回前去除密码
        delete user.password_aes;//返回前去除密码
        delete user.password_md5;//返回前去除密码
        res.json(user);//成功 200
    } catch (e) {
        res.status(500).send({
            error: '账号密码无效'
        })
    }
});

module.exports = router;